package org.sang.controller.mbr;

import org.apache.poi.ss.formula.functions.T;
import org.sang.bean.MbrCard;
import org.sang.bean.RespBean;
import org.sang.service.MbrCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

import static org.springframework.http.ResponseEntity.*;

/**
 * Created by sang on 2018/1/12.
 */
@RestController
@RequestMapping("/mbr/card/basic")
public class MbrCardBasicController {
    @Autowired
    MbrCardService mbrCardService;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public RespBean addMbrCard(@RequestBody MbrCard mbrCard) {
        if (mbrCardService.addMbrCard(mbrCard) == 1) {
            return new RespBean("success", "保存成功!");
        }
        return new RespBean("error", "保存失败!");
    }


    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public RespBean updateMbrCard(@RequestBody MbrCard mbrCard) {
        if (mbrCardService.updateMbrCard(mbrCard) == 1) {
            return new RespBean("success", "更新成功!");
        }
        return new RespBean("error", "更新失败!");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public RespBean deleteMbrCard(@PathVariable(name = "id") String id) {
        if (mbrCardService.deleteMbrCard(id) == 1) {
            return new RespBean("success", "删除成功!");
        }
        return new RespBean("error", "删除失败!");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity queryMbrCard(@PathVariable(name = "id") String id) {
        MbrCard mbrCard = mbrCardService.queryMbrCard(id);
        if (Objects.isNull(mbrCard)) {
            return badRequest().body(new RespBean("error", "资源不存在!"));
        }
        return ok(mbrCard);
    }

    @RequestMapping(value = "/collection", method = RequestMethod.GET)
    public ResponseEntity queryMbrCards(MbrCard mbrCard) {
        List<MbrCard> mbrCards = mbrCardService.queryMbrCards(mbrCard);
        return ok(mbrCards);
    }

}
